package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.ImgUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.RatingBar;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.view.CircleImageView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.AllEvaluationModel;
import com.diudiutechnology.www.yadiudiu.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/19.
 * 评论
 */

public class TotalEvaluationAdapter extends BaseAdapter {

    private Context mContext;

    List<AllEvaluationModel> model= new ArrayList<>();
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    public TotalEvaluationAdapter(Context context) {
        this.mContext = context;
        options = ImgUtils.mImageOptions();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    }

    @Override
    public int getCount() {
        return model.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    public void addItem(AllEvaluationModel models){
        this.model.add(models);
        notifyDataSetChanged();
    }

    public void emptying(){
        this.model.clear();
        notifyDataSetChanged();
    }
    public int number(){
        return   this.model.size();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_total_evaluation, null);

            holder.item_title = (TextView) convertView
                    .findViewById(R.id.item_title);
            holder.rating = (RatingBar) convertView
                    .findViewById(R.id.rating);
            holder.item_time = (TextView) convertView
                    .findViewById(R.id.item_time);
            holder.item_content = (TextView) convertView
                    .findViewById(R.id.item_content);
            holder.item_standard = (TextView) convertView
                    .findViewById(R.id.item_standard);
            holder.item_image = (CircleImageView) convertView
                    .findViewById(R.id.item_image);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        AllEvaluationModel bModel = model.get(position);
        imageLoader.displayImage(Constant.HTTP + bModel.getAvatar(), holder.item_image, options);
        //姓名
        if (!Sundry.getStinges(bModel.getNickname())) {
            holder.item_title.setText(bModel.getNickname());
        } else {
            //电话
            if (!Sundry.getStinges(bModel.getPhone())) {
                String string = Sundry.getphon(bModel.getPhone()).toString();
                holder.item_title.setText(string);
            }
        }

        holder.item_time.setText(Sundry.getStrTime(3,bModel.getCommenttime()));
        holder.item_content.setText(bModel.getComment());
        holder.item_standard.setText("");
        if (!Sundry.getStinges(bModel.getStar())){
            holder.rating.setStar(Float.valueOf(bModel.getStar()));//设置显示的星星个数
        }else {
            holder.rating.setStar(0f);//设置显示的星星个数
        }
        return convertView;
    }

    class ViewHolder {

        TextView item_title;
        RatingBar rating;
        TextView item_time;
        TextView item_content;
        TextView item_standard;
        CircleImageView item_image;
    }
}

